package com.ruoyi.teach.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.teach.mapper.TeacherMapper;
import com.ruoyi.teach.domain.Teacher;
import com.ruoyi.teach.service.ITeacherService;

/**
 * 教师管理Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-22
 */
@Service
public class TeacherServiceImpl implements ITeacherService
{
    @Autowired
    private TeacherMapper teacherMapper;

    private static final Logger log = LoggerFactory.getLogger(TeacherServiceImpl.class);

    /**
     * 查询教师管理
     *
     * @param teacherId 教师管理主键
     * @return 教师管理
     */
    @Override
    public Teacher selectTeacherByTeacherId(Long teacherId)
    {
        return teacherMapper.selectTeacherByTeacherId(teacherId);
    }

    /**
     * 查询教师管理列表
     *
     * @param teacher 教师管理
     * @return 教师管理
     */
    @Override
    public List<Teacher> selectTeacherList(Teacher teacher)
    {
        return teacherMapper.selectTeacherList(teacher);
    }

    /**
     * 新增教师管理
     *
     * @param teacher 教师管理
     * @return 结果
     */
    @Override
    public int insertTeacher(Teacher teacher)
    {
        teacher.setCreateTime(DateUtils.getNowDate());
        return teacherMapper.insertTeacher(teacher);
    }

    /**
     * 修改教师管理
     *
     * @param teacher 教师管理
     * @return 结果
     */
    @Override
    public int updateTeacher(Teacher teacher)
    {
        teacher.setUpdateTime(DateUtils.getNowDate());
        return teacherMapper.updateTeacher(teacher);
    }

    /**
     * 批量删除教师管理
     *
     * @param teacherIds 需要删除的教师管理主键
     * @return 结果
     */
    @Override
    public int deleteTeacherByTeacherIds(Long[] teacherIds)
    {
        return teacherMapper.deleteTeacherByTeacherIds(teacherIds);
    }

    /**
     * 删除教师管理信息
     *
     * @param teacherId 教师管理主键
     * @return 结果
     */
    @Override
    public int deleteTeacherByTeacherId(Long teacherId)
    {
        return teacherMapper.deleteTeacherByTeacherId(teacherId);
    }

    /**
     * 获取教师信息
     *
     * @param teacherId 教师管理主键
     * @return 结果
     */
    public Teacher getTeacherInfo(Teacher teacher)
    {
        return teacherMapper.getTeacherInfo(teacher);
    }

    /**
     * 导入教师管理
     *
     * @param teacherList      教师数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName        操作用户
     * @return 结果
     */
    @Override
    public String importTeacherList(List<Teacher> teacherList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(teacherList) || teacherList.isEmpty()) {
            throw new ServiceException("导入教师数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Teacher teacher : teacherList) {
            try {
                // 验证是否存在这个报名人
                Teacher teacherDb = this.getTeacherInfo(teacher);
                if (StringUtils.isNull(teacherDb)){
                    teacher.setCreateBy(operName);
                    this.insertTeacher(teacher);
                    successNum++;
                    successMsg.append("<br/>").append(successNum).append("、").append(teacher.getTeacherName()).append(" 教师信息导入成功");
                } else if (isUpdateSupport) {
                    teacherDb.setUpdateBy(operName);
                    this.updateTeacher(teacherDb);
                    successNum++;
                    successMsg.append("<br/>").append(successNum).append("、 ").append(teacher.getTeacherName()).append(" 教师信息更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>").append(failureNum).append("、 ").append(teacher.getTeacherName()).append(" 教师信息已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、 " + teacher.getTeacherName() + " 教师信息导入失败：";
                failureMsg.append(msg).append(e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

}

